package com.yellowpage.wf.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;   
  
import org.apache.struts2.ServletActionContext; 
import java.sql.Timestamp;
import com.yellowpage.wf.data.Resource;
import com.yellowpage.wf.service.ResourceService;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.wf.WorkPanelGenerator;
import com.yellowpage.wf.data.Context;
import com.yellowpage.wf.data.NodeContext;
import com.yellowpage.wf.data.Participants;
import com.yellowpage.wf.data.Workflow;
import com.yellowpage.wf.data.WorkflowDataUtils;
import com.yellowpage.wf.data.WorkflowInstance;
import com.yellowpage.wf.data.WorkflowNode;
import com.yellowpage.wf.data.WorkflowPath;
import com.yellowpage.wf.data.Attachment;
import com.yellowpage.wf.data.NodeAttachment;
import com.yellowpage.wf.service.ContextService;
import com.yellowpage.wf.service.NodeContextService;
import com.yellowpage.wf.service.ParticipantsService;
import com.yellowpage.wf.service.WorkflowInstanceService;
import com.yellowpage.wf.service.WorkflowNodeInstanceService;
import com.yellowpage.wf.service.WorkflowNodeService;
import com.yellowpage.wf.service.WorkflowPathService;
import com.yellowpage.wf.service.WorkflowService;
import com.yellowpage.wf.service.AttachmentService;
import com.yellowpage.wf.service.NodeAttachmentService;

public class WorkflowAction extends BaseAction {

	public String RESULT_QUERY = "query";
	//public String RESULT_SUCCESS = "success";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_QEURY_ACTION = "query_action";
	private String RESULT_COPY = "copy";
	private String RESULT_DETAIL = "detail";
	public String RESULT_NODE_ADD = "node_add";
	public String RESULT_NODE_DETAIL = "node_detail";
	public String RESULT_INSTANCE_QUERY = "instance_query";
	public String RESULT_INSTANCE_ADD = "instance_add";
	public String RESULT_INSTANCE_CONTROL = "instance_control";
	public String RESULT_CONTEXT_ADD = "context_add";
	public String RESULT_CONTEXT_EDIT = "context_edit";
	public String RESULT_ATTACHMENT_ADD = "attachment_add";
	public String RESULT_NODE_ATTACHMENT_ADD = "node_attachment_add";
	public String RESULT_FILE_UPLOAD = "file_upload";
	public String RESULT_NODE_FILE_UPLOAD = "node_file_upload";
	public String RESULT_FILE_SUCCESS = "file_success";
	private Configuration configuration;
	
	private List<Workflow> workflowList = new ArrayList<Workflow>();
	private List<Map> resultList = new ArrayList<Map>();
	private List<Map> nodeList = new ArrayList<Map>();
	private List<Map> paramList = new ArrayList<Map>();
	private List<Map> attachmentList = new ArrayList<Map>();
	private List<Map> nodeAttachmentList = new ArrayList<Map>();
	private List<Map> participantTypeList = new ArrayList<Map>();
	private List<Map> participantList = new ArrayList<Map>();
	private List<Map> nodeContextList = new ArrayList<Map>();

	private List<Map> fromNodeList;
	private List<Map> nextNodeList;	

	private Workflow workflow;
	private WorkflowNode workflowNode;
	private WorkflowPath workflowPath;
	private WorkflowInstance workflowInstance;
	private Context workflowContext;
	private Attachment attachment;
	private NodeAttachment nodeAttachment;
	private Resource resource;
	private Constants constants;
	
    private ResourceService resourceService;
	private WorkflowService workflowService;
	private WorkflowNodeService workflowNodeService;
	private WorkflowPathService workflowPathService;
	private WorkflowInstanceService workflowInstanceService;
	private WorkflowNodeInstanceService workflowNodeInstanceService;
	private NodeContextService nodeContextService;
	private ParticipantsService participantsService;
	private ContextService contextService;
	private AttachmentService attachmentService;
	private NodeAttachmentService nodeAttachmentService;
	
	private File file; 
	private String fileFileName; 
	
	private String workflowId;
	private String workflowNodeId;
	private String workflowPathId;
	private String participantId;
	private String nodeContextId;
	private String[] selectedIds;
	private String workId;
	private String workNodeId;
	private String message;
	private String attachmentId;
	private String nodeAttachmentId;
	private String resourceId;

	private String query_workflowId;
	private String query_createUser;
	private String query_createTime;
	private String query_lastUpdateUser;
	private String query_lastUpdateTime;
	private String query_name = "";
	private String query_type = "";
	private String query_status = "";

	private String preWorkflowNodeId = "";
	private String afterWorkflowNodeId = "";
	private String nextWorkflowNodeId = "";
	private String contextId = "";
	private String nextPathName = "";
	private String fromPathName = "";
	
	private String participantType = "";
	private String participantValue = "";
	
	private String nodeParamType = "";		
	private String nodeParamName = "";
	private String nodeParamDefaultValue = "";
	
	private String workflowContextParamName = "";
	private String workflowContextParamType = "";
	private String workflowContextDefaultValue = "";

	private Map workflowStatusMap;
	private Map workflowNodeWorkTypeMap;
	private Map workflowInstanceStatusMap;
	private List<Map> workflowNodeInstanceMapList;
	private Map workflowContextTypeMap;

	public void prepare() {
		super.prepare();

		workflowStatusMap = WorkflowDataUtils.takeWorkflowStatusMap();
		workflowNodeWorkTypeMap = WorkflowDataUtils
				.takeWorkflowNodeWorkTypeMap();
		workflowInstanceStatusMap = WorkflowDataUtils
				.takeWorkflowInstanceStatusMap();
		workflowContextTypeMap = WorkflowDataUtils.getWorkflowContextTypeMap();	
		
		Map participantType = new HashMap();
		participantType.put("PARTICIPANT_TYPE_NAME", "用户");
		participantType.put("PARTICIPANT_TYPE", "USER");
		participantTypeList.add(participantType);
		participantType = new HashMap();
		participantType.put("PARTICIPANT_TYPE_NAME", "角色");
		participantType.put("PARTICIPANT_TYPE", "ROLE");
		participantTypeList.add(participantType);
		participantType = new HashMap();
		participantType.put("PARTICIPANT_TYPE_NAME", "SQL");
		participantType.put("PARTICIPANT_TYPE", "SQL");
		participantTypeList.add(participantType);

		if (this.workflowId != null && !this.workflowId.equals("")) {
			this.workflow = workflowService.getWorkflow(Long
					.parseLong(workflowId));
		}
		
		if (this.workflowNodeId != null && !this.workflowNodeId.equals("")) {
			this.workflowNode = workflowNodeService.getWorkflowNode(Long
					.parseLong(workflowNodeId));
		}
		if (this.workflowPathId != null && !this.workflowPathId.equals("")) {
			this.workflowPath = workflowPathService.getWorkflowPath(Long
					.parseLong(workflowPathId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("name", "%" + query_name.trim() + "%");
		queryMap.put("type", "%" + query_type.trim() + "%");
		queryMap.put("status", "%" + query_status.trim() + "%");

		resultList = workflowService.queryWorkflowByPage(queryMap, pageRender);

		setPageRenderToRequest();
		return RESULT_QUERY;
	}

	public String showDetail() {

		initPageRender();

		Long id = new Long(workflowId);
		
		workflow = workflowService.getWorkflow(id);

		Map queryMap = new HashMap();
		queryMap.put("workflowId", workflowId);

		nodeList = workflowNodeService.queryWorkflowNodeByPage(queryMap,
				pageRender);

		paramList = workflowService.getWorkflowContext(queryMap);
		attachmentList = attachmentService.getAttachmentOfWorkflow(new Long(workflowId));
		
		log.error("^^^^^^^^^^ WorkflowAction showDetail 02" + paramList.size());
		return RESULT_DETAIL;
	}

	public String update() {
		workflow.setLastUpdateTime(new Date());
		workflow.setLastUpdateUser(loginUser.getUserId());
		try {
			workflowService.updateWorkflow(workflow);
			message = "工作流信息更新成功!";
		} catch (Exception e) {
			message = "工作流信息更新失败!";
		}

		addActionMessage(message);
		return showDetail();
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String toCopy() {
		return RESULT_COPY;
	}

	public String copy() {
		Workflow newWorkflow = new Workflow();
		Workflow originalWorkflow = workflowService.getWorkflow(Long
				.valueOf(workflowId));
		newWorkflow.setName(this.workflow.getName());
		workflowService.workflowCopy(originalWorkflow, newWorkflow);
		message = "复制成功";
		addActionMessage(message);
		return RESULT_QEURY_ACTION;
	}

	public String save() {
		workflow.setCreateTime(new Date());
		workflow.setCreateUser(loginUser.getUserId());
		workflow.setLastUpdateTime(new Date());
		workflow.setLastUpdateUser(loginUser.getUserId());

		try {
			workflowService.saveWorkflow(workflow);
			message = "工作流新增成功!";
		} catch (Exception e) {
			message = "工作流新增失败!";
		}

		addActionMessage(message);

		return RESULT_ADD;
	}

	public void validateSave() {
		if (workflow.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
		if (workflow.getType().length() == 0) {
			this.addActionError("** 类型 不能为空 !");
		}
		this.setValidatorResult("/wf/WorkflowAdd.jsp");
	}

	public void validateCopy() {
		if (workflow.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
		this.setValidatorResult("/wf/WorkflowCopy.jsp");
	}

	public void validateUpdate() {
		if (workflow.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
		if (workflow.getType().length() == 0) {
			this.addActionError("** 类型 不能为空 !");
		}
		this.setValidatorResult("/wf/WorkflowUpdate.jsp");
	}

	public String delete() {
		List<Long> ids = new ArrayList();

		for (String id : selectedIds) {
			String workflowName = workflowService.isWorkflowUsed(Long
					.valueOf(id));
			if (workflowName != null) {
				message = "有工作流实例正在使用 " + workflowName + " 工作流，不能删除!";
				addActionMessage(message);
			} else {
				ids.add(Long.valueOf(id));
			}
		}
		List<Map> resultMapList = null;
		try {
			resultMapList = workflowService.deleteWorkflow(ids);
			selectedIds = null;
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 删除失败,记录可能还有约束冲突 !");
		}

		return queryByPage();
	}

	public String toNodeAdd() {
		workflowNode = new WorkflowNode();
		workflow = workflowService.getWorkflow(new Long(workId));
		workflowNode.setWorkflowId(workflow.getWorkflowId());

		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("workflowId", workflow.getWorkflowId().toString());

		nodeList = workflowService.getWorkflowNodes(queryMap);

		return RESULT_NODE_ADD;
	}
	
	public String saveNode() {

		workflowNode.setWorkflowId(workflow.getWorkflowId());

		workflowNode.setCreateTime(new Date());
		workflowNode.setCreateUser(loginUser.getUserId());
		workflowNode.setLastUpdateTime(new Date());
		workflowNode.setLastUpdateUser(loginUser.getUserId());

		try {
			Long preNodeId = null;
			Long afterNodeId = null;

			if (preWorkflowNodeId != null && preWorkflowNodeId.length() > 0) {
				preNodeId = new Long(preWorkflowNodeId);
			}

			if (afterWorkflowNodeId != null && afterWorkflowNodeId.length() > 0) {
				afterNodeId = new Long(afterWorkflowNodeId);
			}

			workflowService.saveWorkflowNode(workflowNode, preNodeId,
					afterNodeId);

			message = "工作流节点新增成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流节点新增失败!";
		}

		addActionMessage(message);
		workId = workflowNode.getWorkflowId().toString();
		return toNodeAdd();
	}

	public String showNodeDetail() {
		Long id = null;
		if (this.workNodeId != null && !this.workNodeId.equals(""))
		{
			id = new Long(workNodeId);
		}
		else
		{
			id = new Long(workflowNodeId);
		}
		workflowNode = workflowNodeService.getWorkflowNode(id);
		workflow = workflowService.getWorkflow(workflowNode.getWorkflowId());

		Map queryMap = new HashMap();
		queryMap.put("workflowId", workflowNode.getWorkflowId().toString());

		nodeList = workflowService.getWorkflowNodes(queryMap);

		queryMap.put("workflowNodeId", workflowNodeId);
		paramList = workflowService.getWorkflowNodeContext(queryMap);

		fromNodeList = workflowService.getFromNodeList(id);
		nextNodeList = workflowService.getNextNodeList(id);
		
		participantList = participantsService.getParticipantsByNode(id);
		
		nodeContextList = nodeContextService.getNodeContextByNode(id);
		nodeAttachmentList = nodeAttachmentService.getNodeAttachmentOfNode(id);
		return RESULT_NODE_DETAIL;
	}

	public String addFromNode() {
		WorkflowPath fromPath = new WorkflowPath();
		if ((preWorkflowNodeId != null) && (!preWorkflowNodeId.equals(""))){
		fromPath.setNodeFrom(new Long(preWorkflowNodeId));
		}
		fromPath.setNodeTo(new Long(workflowNodeId));
		fromPath.setDisplayName(fromPathName);
		fromPath.setWorkflowId(new Long(workflowId));
		fromPath.setCreateTime(new Date());
		fromPath.setCreateUser(loginUser.getUserId());
		fromPath.setLastUpdateTime(new Date());
		fromPath.setLastUpdateUser(loginUser.getUserId());

		workflowPathService.saveWorkflowPath(fromPath);
		return showNodeDetail();
	}

	public String removeFromNode() {
		List deleteList = new ArrayList();
		deleteList.add(this.workflowPath.getWorkflowPathId());
		workflowPathService.delete(deleteList);
		return showNodeDetail();
	}

	public String addNextNode() {
		WorkflowPath nextPath = new WorkflowPath();
		nextPath.setNodeFrom(workflowNode.getWorkflowNodeId());
		nextPath.setNodeTo(new Long(afterWorkflowNodeId));
		nextPath.setDisplayName(nextPathName);
		nextPath.setWorkflowId(new Long(workflowId));
		nextPath.setCreateTime(new Date());
		nextPath.setCreateUser(loginUser.getUserId());
		nextPath.setLastUpdateTime(new Date());
		nextPath.setLastUpdateUser(loginUser.getUserId());

		workflowPathService.saveWorkflowPath(nextPath);

		return showNodeDetail();
	}

	public String removeNextNode() {
		List deleteList = new ArrayList();
		deleteList.add(this.workflowPath.getWorkflowPathId());
		workflowPathService.delete(deleteList);
		return showNodeDetail();
	}
	
	public String addParticipant() {
		Participants participant = new Participants();
		participant.setWorkflowNodeId(workflowNode.getWorkflowNodeId());
		participant.setParticipantName(participantType);		
		participant.setParticipantType(participantType);
		participant.setParticipantValue(participantValue);
		

		participantsService.saveParticipants(participant);

		return showNodeDetail();
	}

	public String removeParticipant() {
		log.error("############### removeParticipant:" + participantId);
		List deleteList = new ArrayList();
		deleteList.add(new Long(participantId));
		participantsService.delete(deleteList);
		
		return showNodeDetail();
	}
	
	public String addNodeContext() {
		NodeContext nodeContext = new NodeContext();
		nodeContext.setWorkflowNodeId(workflowNode.getWorkflowNodeId());
		nodeContext.setParamType(nodeParamType);		
		nodeContext.setParamName(nodeParamName);
		nodeContext.setDefaultValue(nodeParamDefaultValue);
		

		nodeContextService.saveNodeContext(nodeContext);

		return showNodeDetail();
	}

	public String removeNodeContext() {
		List deleteList = new ArrayList();
		deleteList.add(new Long(nodeContextId));
		nodeContextService.delete(deleteList);
		
		return showNodeDetail();
	}

	public String updateNode() {
		workflowNode = workflowNodeService.getWorkflowNode(new Long(workNodeId));
		workflowNode.setLastUpdateTime(new Date());
		workflowNode.setLastUpdateUser(loginUser.getUserId());
		workflowNode.setWorkflowId(new Long(workId));
		try {
			workflowNodeService.updateWorkflowNode(workflowNode);
			message = "工作流节点信息更新成功!";
		} catch (Exception e) {
			message = "工作流节点信息更新失败!";
		}

		addActionMessage(message);
		return showNodeDetail();
	}

	public String getInstanceByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("workflowId", query_workflowId);
		queryMap.put("status", query_status);

		resultList = workflowService.getWorkflowInstanceByPage(queryMap,
				pageRender);

		setPageRenderToRequest();
		workflowList = workflowService.getAllWorkflow();
		return RESULT_INSTANCE_QUERY;
	}

	public String startInstance() {
		WorkflowInstance workflowInstance = new WorkflowInstance();
		workflowInstance.setWorkflowId(Long.valueOf(query_workflowId));
		workflowInstance.setStatus("on");
		workflowInstance.setCreateTime(new Date());
		workflowInstance.setCreateUser(loginUser.getUserId());
		workflowInstance.setLastUpdateTime(new Date());
		workflowInstance.setLastUpdateUser(loginUser.getUserId());
		try {
			workflowService.createWorkflowInstance(workflowInstance);
			message = "工作流启动成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流启动失败!";
			addActionMessage(message);
			return RESULT_INSTANCE_ADD;
		}
		addActionMessage(message);
		workflowList = workflowService.getAllWorkflow();
		query_workflowId = "";
		return RESULT_INSTANCE_ADD;
	}

	public String toInstanceControl() {
		Long instanceId = new Long(workflowId);

		workflowInstance = workflowInstanceService
				.getWorkflowInstance(instanceId);
		workflow = workflowService
				.getWorkflow(workflowInstance.getWorkflowId());

		workflowNode = workflowNodeService.getWorkflowNode(workflowInstance
				.getWorkflowNodeId());
		workflowNodeInstanceMapList = workflowService
				.getNodeInstanceList(workflowInstance.getWorkflowInstanceId());
		nodeList = workflowService.getNextNodeList(workflowInstance
				.getWorkflowNodeId());

		return RESULT_INSTANCE_CONTROL;
	}
	private String workflowInstanceId;
	
	public String getWorkflowInstanceId() {
		return workflowInstanceId;
	}

	public void setWorkflowInstanceId(String workflowInstanceId) {
		this.workflowInstanceId = workflowInstanceId;
	}

	public void goNext() {
		Long instanceId = new Long(workflowInstanceId);
		log.error("instanceId="+instanceId);
		try {

			workflowService.saveInstanceToNext(instanceId, new Long(
					nextWorkflowNodeId));

			message = "工作流操作成功!";

		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流操作失败!";
		}

		workflowInstance = workflowInstanceService
				.getWorkflowInstance(instanceId);
		workflow = workflowService
				.getWorkflow(workflowInstance.getWorkflowId());

		workflowNode = workflowNodeService.getWorkflowNode(workflowInstance
				.getWorkflowNodeId());
		workflowNodeInstanceMapList = workflowService
				.getNodeInstanceList(workflowInstance.getWorkflowInstanceId());
		nodeList = workflowService.getNextNodeList(workflowInstance
				.getWorkflowNodeId());

		//return RESULT_INSTANCE_CONTROL;
	}

	public String toContextAdd() {
		workflowContext = new Context();
		log.error("wf_id=="+workId);
		workflow = workflowService.getWorkflow(new Long(workId));
		workflowContext.setWorkflowId(workflow.getWorkflowId());

		return RESULT_CONTEXT_ADD;
	}
	
	public String saveContext() {
		workflowContext.setWorkflowId(new Long(workId));
		try {
			
			contextService.saveContext(workflowContext);

			message = "工作流参数新增成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流参数新增失败!";
		}

		addActionMessage(message);

		return toContextAdd();
	}
	
	public String toContextEdit() {
		Long cId = new Long(contextId);
		Long Id = new Long(workflowId);
		workflowContext = contextService.getContext(cId);
		workflow = workflowService.getWorkflow(Id);
		return RESULT_CONTEXT_EDIT;
	}
	
	public String editContext() {
		log.error("contextId=="+workflowContext.getContextId());
		Long cId = workflowContext.getContextId();
		workflowContext.setWorkflowId(workflow.getWorkflowId());
		try { 
			
			contextService.updateContext(workflowContext);

			message = "工作流参数修改成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流参数修改失败!";
		}

		addActionMessage(message);

		initPageRender();
		workflowContext = contextService.getContext(cId);
		workflow = workflowService.getWorkflow(workflowContext.getWorkflowId());

		Map queryMap = new HashMap();
		queryMap.put("workflowId", workflow.getWorkflowId().toString());

		nodeList = workflowNodeService.queryWorkflowNodeByPage(queryMap,
				pageRender);

		paramList = workflowService.getWorkflowContext(queryMap);
		return RESULT_DETAIL;
	}
	
	public String deleteContext() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			contextService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 删除失败,记录可能还有约束冲突 !");
		}
		
		
		return queryByPage();
	}
	
	public String deleteAttachment(){
		List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			attachmentService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 删除失败,记录可能还有约束冲突 !");
		}
		
		
		return queryByPage();
	}
	
	public String toAddAttachment()
	{
		attachment = new Attachment();
		workflow = workflowService.getWorkflow(new Long(workId));
		attachment.setWorkflowId(workflow.getWorkflowId());
		return RESULT_ATTACHMENT_ADD;
	}
	public String saveAttachment()
	{
		attachment.setWorkflowId(new Long(workId));
		attachment.setCreateTime(new Date());
		attachment.setCreateUser(loginUser.getUserId());
		try {
			
			attachmentService.saveAttachment(attachment);

			message = "工作流附件新增成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流附件新增失败!";
		}

		addActionMessage(message);
		return toAddAttachment();
	}
	
	public String deleteNodeAttachment(){
		List ids = new ArrayList();
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			nodeAttachmentService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 删除失败,记录可能还有约束冲突 !");
		}
		
		
		return queryByPage();
	}
	
	public String toAddNodeAttachment()
	{
		nodeAttachment = new NodeAttachment();
		log.error("nodeId==="+workNodeId);
		workflowNode = workflowNodeService.getWorkflowNode(new Long(workNodeId));
		nodeAttachment.setWorkflowNodeId(workflowNode.getWorkflowNodeId());
		return RESULT_NODE_ATTACHMENT_ADD;
	}
	public String saveNodeAttachment()
	{
		log.error("nodeId(saving)==="+workNodeId);
		nodeAttachment.setWorkflowNodeId(new Long(workNodeId));
		nodeAttachment.setCreateTime(new Date());
		nodeAttachment.setCreateUser(loginUser.getUserId());
		try {
			
			nodeAttachmentService.saveNodeAttachment(nodeAttachment);

			message = "节点附件新增成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "节点附件新增失败!";
		}

		addActionMessage(message);
		return toAddNodeAttachment();
	}
	public String toUpload(){
		List ids = new ArrayList();
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		Long aid = Long.parseLong(ids.get(0).toString());
		log.error("attachmentID="+aid);
		attachment = attachmentService.getAttachment(aid);
		workflow = workflowService.getWorkflow(attachment.getWorkflowId());
		return RESULT_FILE_UPLOAD;
	}
	public String toNodeUpload(){
		List ids = new ArrayList();
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		Long aid = Long.parseLong(ids.get(0).toString());
		log.error("nodeAttachmentID="+aid);
		nodeAttachment = nodeAttachmentService.getNodeAttachment(aid);
		workflowNode = workflowNodeService.getWorkflowNode(nodeAttachment.getWorkflowNodeId());
		return RESULT_NODE_FILE_UPLOAD;
	}
	
	//执行上传功能   
    public String uploadFile() throws FileNotFoundException, IOException {   
        try {   
            InputStream in = new FileInputStream(file); 
            String dir = constants.get("DIR").toString();  
            String originalFileName = this.getFileFileName().toString(); //原始文件名
            String type = originalFileName.substring(originalFileName.lastIndexOf(".")+1); //文件类型
            String d = new Timestamp(System.currentTimeMillis()).toString();
            String finalFileName = d.substring(0,10).replaceAll("-","")+d.substring(11,19).replaceAll(":","")+d.substring(20);
            File uploadFile = new File(dir,finalFileName+"."+type);   
            OutputStream out = new FileOutputStream(uploadFile);   
            byte[] buffer = new byte[1024 * 1024];   
            int length;   
            while ((length = in.read(buffer)) > 0) {   
                out.write(buffer, 0, length);   
            } 
            //添加resource表的记录
            Resource resource = new Resource();
            resource.setCreateTime(new Date());
            resource.setName(originalFileName.substring(0,originalFileName.lastIndexOf(".")));
            resource.setType(type);
            resource.setDir(dir+finalFileName+"."+type);
            resourceService.saveResource(resource);
            
          //更新attachment表的记录
            attachment = attachmentService.getAttachment(new Long(attachmentId));
            attachment.setResourceId(resource.getResourceId());
            attachmentService.updateAttachment(attachment);
            in.close();   
            out.close();   
        } catch (FileNotFoundException ex) {   
            ex.printStackTrace();   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        }  
        return showDetail();
    }   
    
    public String uploadNodeFile() throws FileNotFoundException, IOException {   
        try {   
            InputStream in = new FileInputStream(file); 
            String dir = constants.get("DIR").toString();  
            String originalFileName = this.getFileFileName().toString(); //原始文件名
            String type = originalFileName.substring(originalFileName.lastIndexOf(".")+1); //文件类型
            String d = new Timestamp(System.currentTimeMillis()).toString();
            String finalFileName = d.substring(0,10).replaceAll("-","")+d.substring(11,19).replaceAll(":","")+d.substring(20);
            File uploadFile = new File(dir,finalFileName+"."+type);   
            OutputStream out = new FileOutputStream(uploadFile);   
            byte[] buffer = new byte[1024 * 1024];   
            int length;   
            while ((length = in.read(buffer)) > 0) {   
                out.write(buffer, 0, length);   
            } 
            //添加resource表的记录
            Resource resource = new Resource();
            resource.setCreateTime(new Date());
            resource.setName(originalFileName.substring(0,originalFileName.lastIndexOf(".")));
            resource.setType(type);
            resource.setDir(dir+finalFileName+"."+type);
            resourceService.saveResource(resource);
            
          //更新attachment表的记录
            nodeAttachment = nodeAttachmentService.getNodeAttachment(new Long(nodeAttachmentId));
            nodeAttachment.setResourceId(resource.getResourceId());
            nodeAttachmentService.updateNodeAttachment(nodeAttachment);
            in.close();   
            out.close();   
        } catch (FileNotFoundException ex) {   
            ex.printStackTrace();   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        }  
        return showNodeDetail();
    }   
//    public String downloadFile() {
//    	log.error("resId="+resourceId);
//    	Resource resource = new Resource();
//    	resource = resourceService.getResource(new Long(resourceId));
//        InputStream inputStream =   
//    ServletActionContext.getServletContext().getResourceAsStream(resource.getDir());  
//        return "down_success";
//    }
	/**
	 * getters and setters
	 * 
	 * @return
	 */

	public WorkflowNode getWorkflowNode() {
		return workflowNode;
	}

	public void setWorkflowNode(WorkflowNode workflowNode) {
		this.workflowNode = workflowNode;
	}

	public void setWorkflow(Workflow workflow) {
		this.workflow = workflow;
	}

	public Workflow getWorkflow() {
		return workflow;
	}

	public List<Workflow> getWorkflowList() {
		return workflowList;
	}

	public void setWorkflowList(List<Workflow> workflowList) {
		this.workflowList = workflowList;
	}

	public String getSelectedId() {
		return workflowId;
	}

	public void setSelectedId(String selectedId) {
		this.workflowId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_workflowId() {
		return this.query_workflowId;
	}

	public void setQuery_workflowId(String workflowId) {
		this.query_workflowId = workflowId;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastUpdateUser() {
		return this.query_lastUpdateUser;
	}

	public void setQuery_lastUpdateUser(String lastUpdateUser) {
		this.query_lastUpdateUser = lastUpdateUser;
	}

	public String getQuery_lastUpdateTime() {
		return this.query_lastUpdateTime;
	}

	public void setQuery_lastUpdateTime(String lastUpdateTime) {
		this.query_lastUpdateTime = lastUpdateTime;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_type() {
		return this.query_type;
	}

	public void setQuery_type(String type) {
		this.query_type = type;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public WorkflowService getWorkflowService() {
		return workflowService;
	}

	public void setWorkflowService(WorkflowService workflowService) {
		this.workflowService = workflowService;
	}

	public List<Map> getNodeList() {
		return nodeList;
	}

	public void setNodeList(List<Map> nodeList) {
		this.nodeList = nodeList;
	}

	public List<Map> getParamList() {
		return paramList;
	}

	public void setParamList(List<Map> paramList) {
		this.paramList = paramList;
	}

	public WorkflowNodeService getWorkflowNodeService() {
		return workflowNodeService;
	}

	public void setWorkflowNodeService(WorkflowNodeService workflowNodeService) {
		this.workflowNodeService = workflowNodeService;
	}

	public Map getWorkflowStatusMap() {
		return workflowStatusMap;
	}

	public void setWorkflowStatusMap(Map workflowStatusMap) {
		this.workflowStatusMap = workflowStatusMap;
	}

	public Map getWorkflowNodeWorkTypeMap() {
		return workflowNodeWorkTypeMap;
	}

	public void setWorkflowNodeWorkTypeMap(Map workflowNodeWorkTypeMap) {
		this.workflowNodeWorkTypeMap = workflowNodeWorkTypeMap;
	}

	public String getPreWorkflowNodeId() {
		return preWorkflowNodeId;
	}

	public void setPreWorkflowNodeId(String preWorkflowNodeId) {
		this.preWorkflowNodeId = preWorkflowNodeId;
	}

	public String getAfterWorkflowNodeId() {
		return afterWorkflowNodeId;
	}

	public void setAfterWorkflowNodeId(String afterWorkflowNodeId) {
		this.afterWorkflowNodeId = afterWorkflowNodeId;
	}

	public WorkflowPathService getWorkflowPathService() {
		return workflowPathService;
	}

	public void setWorkflowPathService(WorkflowPathService workflowPathService) {
		this.workflowPathService = workflowPathService;
	}

	public Map getWorkflowInstanceStatusMap() {
		return workflowInstanceStatusMap;
	}

	public void setWorkflowInstanceStatusMap(Map workflowInstanceStatusMap) {
		this.workflowInstanceStatusMap = workflowInstanceStatusMap;
	}

	public WorkflowInstance getWorkflowInstance() {
		return workflowInstance;
	}

	public void setWorkflowInstance(WorkflowInstance workflowInstance) {
		this.workflowInstance = workflowInstance;
	}

	public WorkflowInstanceService getWorkflowInstanceService() {
		return workflowInstanceService;
	}

	public void setWorkflowInstanceService(
			WorkflowInstanceService workflowInstanceService) {
		this.workflowInstanceService = workflowInstanceService;
	}

	public WorkflowNodeInstanceService getWorkflowNodeInstanceService() {
		return workflowNodeInstanceService;
	}

	public void setWorkflowNodeInstanceService(
			WorkflowNodeInstanceService workflowNodeInstanceService) {
		this.workflowNodeInstanceService = workflowNodeInstanceService;
	}

	public List<Map> getWorkflowNodeInstanceMapList() {
		return workflowNodeInstanceMapList;
	}

	public void setWorkflowNodeInstanceMapList(
			List<Map> workflowNodeInstanceMapList) {
		this.workflowNodeInstanceMapList = workflowNodeInstanceMapList;
	}

	public String getNextWorkflowNodeId() {
		return nextWorkflowNodeId;
	}

	public void setNextWorkflowNodeId(String nextWorkflowNodeId) {
		this.nextWorkflowNodeId = nextWorkflowNodeId;
	}

	public List<Map> getFromNodeList() {

		return fromNodeList;
	}

	public void setFromNodeList(List<Map> fromNodeList) {
		this.fromNodeList = fromNodeList;
	}

	public List<Map> getNextNodeList() {
		return nextNodeList;
	}

	public void setNextNodeList(List<Map> nextNodeList) {
		this.nextNodeList = nextNodeList;
	}

	public String getWorkflowNodeId() {
		return workflowNodeId;
	}

	public void setWorkflowNodeId(String workflowNodeId) {
		this.workflowNodeId = workflowNodeId;
	}

	public String getNextPathName() {
		return nextPathName;
	}

	public void setNextPathName(String nextPathName) {
		this.nextPathName = nextPathName;
	}

	public String getFromPathName() {
		return fromPathName;
	}

	public void setFromPathName(String fromPathName) {
		this.fromPathName = fromPathName;
	}

	public String getWorkflowId() {
		return workflowId;
	}

	public void setWorkflowId(String workflowId) {
		this.workflowId = workflowId;
	}

	public String getWorkflowPathId() {
		return workflowPathId;
	}

	public void setWorkflowPathId(String workflowPathId) {
		this.workflowPathId = workflowPathId;
	}

	public WorkflowPath getWorkflowPath() {
		return workflowPath;
	}

	public void setWorkflowPath(WorkflowPath workflowPath) {
		this.workflowPath = workflowPath;
	}

	public List<Map> getParticipantTypeList() {
		return participantTypeList;
	}

	public void setParticipantTypeList(List<Map> participantTypeList) {
		this.participantTypeList = participantTypeList;
	}

	public String getParticipantId() {
		return participantId;
	}

	public void setParticipantId(String participantId) {
		this.participantId = participantId;
	}

	public String getParticipantType() {
		return participantType;
	}

	public void setParticipantType(String participantType) {
		this.participantType = participantType;
	}

	public String getParticipantTypeValue() {
		return participantValue;
	}

	public void setParticipantTypeValue(String participantTypeValue) {
		this.participantValue = participantTypeValue;
	}

	public List<Map> getParticipantList() {
		return participantList;
	}

	public void setParticipantList(List<Map> participantList) {
		this.participantList = participantList;
	}

	public ParticipantsService getParticipantsService() {
		return participantsService;
	}

	public void setParticipantsService(ParticipantsService participantsService) {
		this.participantsService = participantsService;
	}

	public String getParticipantValue() {
		return participantValue;
	}

	public void setParticipantValue(String participantValue) {
		this.participantValue = participantValue;
	}

	public List<Map> getNodeContextList() {
		return nodeContextList;
	}

	public void setNodeContextList(List<Map> nodeContextList) {
		this.nodeContextList = nodeContextList;
	}

	public NodeContextService getNodeContextService() {
		return nodeContextService;
	}

	public void setNodeContextService(NodeContextService nodeContextService) {
		this.nodeContextService = nodeContextService;
	}

	public String getNodeContextId() {
		return nodeContextId;
	}

	public void setNodeContextId(String nodeContextId) {
		this.nodeContextId = nodeContextId;
	}

	public String getNodeParamType() {
		return nodeParamType;
	}

	public void setNodeParamType(String nodeParamType) {
		this.nodeParamType = nodeParamType;
	}

	public String getNodeParamName() {
		return nodeParamName;
	}

	public void setNodeParamName(String nodeParamName) {
		this.nodeParamName = nodeParamName;
	}

	public String getNodeParamDefaultValue() {
		return nodeParamDefaultValue;
	}

	public void setNodeParamDefaultValue(String nodeParamDefaultValue) {
		this.nodeParamDefaultValue = nodeParamDefaultValue;
	}

	public Context getWorkflowContext() {
		return workflowContext;
	}

	public void setWorkflowContext(Context workflowContext) {
		this.workflowContext = workflowContext;
	}

	public ContextService getContextService() {
		return contextService;
	}

	public void setContextService(ContextService contextService) {
		this.contextService = contextService;
	}

	public String getWorkflowContextParamName() {
		return workflowContextParamName;
	}

	public void setWorkflowContextParamName(String workflowContextParamName) {
		this.workflowContextParamName = workflowContextParamName;
	}

	public String getWorkflowContextParamType() {
		return workflowContextParamType;
	}

	public void setWorkflowContextParamType(String workflowContextParamType) {
		this.workflowContextParamType = workflowContextParamType;
	}

	public String getWorkflowContextDefaultValue() {
		return workflowContextDefaultValue;
	}

	public void setWorkflowContextDefaultValue(String workflowContextDefaultValue) {
		this.workflowContextDefaultValue = workflowContextDefaultValue;
	}

	public Map getWorkflowContextTypeMap() {
		return workflowContextTypeMap;
	}

	public void setWorkflowContextTypeMap(Map workflowContextTypeMap) {
		this.workflowContextTypeMap = workflowContextTypeMap;
	}

	public String getContextId() {
		return contextId;
	}

	public void setContextId(String contextId) {
		this.contextId = contextId;
	}

	public List<Map> getAttachmentList() {
		return attachmentList;
	}

	public void setAttachmentList(List<Map> attachmentList) {
		this.attachmentList = attachmentList;
	}

	public Attachment getAttachment() {
		return attachment;
	}

	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}

	public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

	public List<Map> getNodeAttachmentList() {
		return nodeAttachmentList;
	}

	public void setNodeAttachmentList(List<Map> nodeAttachmentList) {
		this.nodeAttachmentList = nodeAttachmentList;
	}

	public NodeAttachment getNodeAttachment() {
		return nodeAttachment;
	}

	public void setNodeAttachment(NodeAttachment nodeAttachment) {
		this.nodeAttachment = nodeAttachment;
	}

	public NodeAttachmentService getNodeAttachmentService() {
		return nodeAttachmentService;
	}

	public void setNodeAttachmentService(NodeAttachmentService nodeAttachmentService) {
		this.nodeAttachmentService = nodeAttachmentService;
	}

	public String getWorkId() {
		return workId;
	}

	public void setWorkId(String workId) {
		this.workId = workId;
	}

	public String getWorkNodeId() {
		return workNodeId;
	}

	public void setWorkNodeId(String workNodeId) {
		this.workNodeId = workNodeId;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getAttachmentId() {
		return attachmentId;
	}

	public void setAttachmentId(String attachmentId) {
		this.attachmentId = attachmentId;
	}

	public String getNodeAttachmentId() {
		return nodeAttachmentId;
	}

	public void setNodeAttachmentId(String nodeAttachmentId) {
		this.nodeAttachmentId = nodeAttachmentId;
	}

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

}
